package com.yanggeng.demo.beginnerguidedemo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.yanggeng.demo.beginnerguidedemo.R;
import com.yanzhenjie.album.Album;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yanggeng on 2017/6/29.
 */

public class PhotoPreviewAdapter extends RecyclerView.Adapter<PhotoPreviewAdapter.PhotoViewHolder> {

    private Context context;
    private List<String> datas;
    private OnItemClickListener onItemClickListener;
    private int itemSize;
    private LayoutInflater inflater;


    public PhotoPreviewAdapter(Context context, List<String> datas, int itemSize) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.datas = datas;
        this.itemSize = itemSize;
    }

    @Override
    public PhotoPreviewAdapter.PhotoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new PhotoViewHolder(inflater.inflate(R.layout.layout_photo_item, parent, false), itemSize);
    }

    @Override
    public void onBindViewHolder(final PhotoPreviewAdapter.PhotoViewHolder holder, final int position) {
        if(datas.get(position).equals("+")) {
            holder.iv_photo.setImageResource(R.mipmap.add_photo);
        } else {
            holder.loadImage(datas.get(position));
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClick(holder.itemView, position, holder.getItemId());
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    public void setData(ArrayList<String> datas) {
        this.datas = datas;
        super.notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    class PhotoViewHolder extends RecyclerView.ViewHolder {

        private ImageView iv_photo;
        private int height;

        public PhotoViewHolder(View itemView, int height) {
            super(itemView);
            iv_photo = (ImageView) itemView.findViewById(R.id.iv_photo);
            this.height = height;
            itemView.getLayoutParams().height = height;
        }

        public void loadImage(String path) {
            Album.getAlbumConfig().getAlbumLoader().load(iv_photo, path);
        }

    }

    public interface OnItemClickListener {
        public void onItemClick(View view, int position, long id);
    }
}
